const PREFIX = "exam";

export const set = (name, value) => {
    localStorage.setItem(`${PREFIX}_${name}`, typeof value === "string" ? value : JSON.stringify(value));
};

export const get = (name) => {
    const item = localStorage.getItem(`${PREFIX}_${name}`);
    let result;
    try {
        result = item === null ? null : JSON.parse(item);
    } catch {
        result = item;
    }
    return result;
};

export const remove = (name) => {
    localStorage.removeItem(`${PREFIX}_${name}`);
};

export const clear = () => {
    Object.keys(localStorage).forEach((name) => {
        const REGEXP = /^exam_(.+)/;
        if (!REGEXP.test(name)) {
            return;
        }
        remove(name.substring(4));
    });
};

export const STORAGE_TYPES = {
    // 储存token
    SET_TOKEN: "SET_TOKEN"
}
